﻿

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 海报api控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PosterController : ControllerBase
    {

        private IHttpContextUser _user;
        private readonly IXrcPosterServices _posterServices;
        private readonly IXrcPosterTypeServices _posterTypeServices;
        private readonly IXrcApplyBrokerServices _xrcApplyBrokerService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="user"></param>
        /// <param name="posterServices"></param>
        /// <param name="posterTypeServices"></param>
        /// <param name="xrcApplyBrokerServices"></param>
        public PosterController(IHttpContextUser user, IXrcPosterServices posterServices, IXrcPosterTypeServices posterTypeServices, IXrcApplyBrokerServices xrcApplyBrokerServices)
        {
            _user = user;
            _posterServices = posterServices;
            _posterTypeServices = posterTypeServices;
            _xrcApplyBrokerService = xrcApplyBrokerServices;
        }





        #region 获取海报列表
        /// <summary>
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<WebApiCallBack> GetPosterList([FromBody] FMPageByIntId entity)
        {
            var jm = new WebApiCallBack();


            int storeId = entity.store > 0 ? entity.store : _user.Store > 0 ? _user.Store : 1;
       
            var list = await _posterServices.QueryPageAsync(p => p.Delete == 0 && p.State == 1 && p.Type == entity.id &&p.GId== storeId, p => p.CreateTime, OrderByType.Desc,
                entity.page, entity.limit);

            var posterType = new List<XrcPosterType>();
            if (_xrcApplyBrokerService.QueryByClauseAsync(p => p.Userid == _user.ID && p.Delete == 0 && p.state == 1 && p.ParentId == 0 && p.Gid == storeId).Result != null)
            {
                posterType = await _posterTypeServices.QueryListByClauseAsync(p => p.Delete == 0 && p.Gid == storeId);
            }
            else
            {
                posterType = await _posterTypeServices.QueryListByClauseAsync(p => p.Id != 3 && p.Delete==0 && p.Gid == storeId);
            }
            var typeName = string.Empty;
            if (posterType.Any())
            {
                var type = posterType.Find(p => p.Id == entity.id);
                typeName = type != null ? type.Type_Name : "";
            }
            jm.status = true;
            jm.data = new
            {
                list,
                posterType,
                type_name = typeName,
                count = list.TotalCount
            };

            return jm;
        }

        #endregion


        /// <summary>
        /// 获取海报详情
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetPostrDetail([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();

            var model = await _posterServices.QueryByIdAsync(entity.id);
            if (model == null)
            {
                jm.msg = "数据获取失败";
                return jm;
            }
            jm.status = true;
            jm.data = model;
            return jm;

        }
    }
}
